﻿using System.Collections.Generic;
using System.Linq;
using JetBrains.ReSharper.Psi.CSharp.Tree;
using Twins.Infrastructure;
using Twins.Interface;
using Twins.Interface.IHandler;

namespace Twins.Classes.Hanlder.HanlderStatement
{
    public class HanlderForIForStatement : IHandler<IForStatement>
    {
        public IEnumerable<ILineCode> DoFor(IForStatement forStatement)
        {
            var receiveHashLineCodeFromProcesserCSharpStatement = IoC
                .Resolve<IReceiveHashLineCodeFromProcesserCSharpStatement>();
            string hashLineCode = forStatement.ForKeyword.GetText();
            hashLineCode = forStatement.InitializerDeclarations
                .Aggregate(hashLineCode, (current, initializerDeclaration) => 
                    current + initializerDeclaration.AssignmentSign.GetText());

            var receiveHashLineCodeFromProcesserCSharpExpression = IoC
                .Resolve<IReceiveHashLineCodeFromProcesserCSharpExpression>();
            receiveHashLineCodeFromProcesserCSharpExpression
                .ReceiveHashLineCode(forStatement.Condition, ref hashLineCode);
            foreach (ICSharpExpression iteratorExpression in forStatement.IteratorExpressions)
                receiveHashLineCodeFromProcesserCSharpExpression
                    .ReceiveHashLineCode(iteratorExpression, ref hashLineCode);
            return receiveHashLineCodeFromProcesserCSharpStatement
                .ReceiveHashLineCode(forStatement, forStatement.Body, hashLineCode);
        }

        public IEnumerable<ILineCode> DoFor(object obj)
        {
            return DoFor((IForStatement) obj);
        }
    }
}